<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    table {
      border-collapse: collapse;
    }
    th, tr, td {
      padding: 10px 16px;
      text-align: center;
      border: 1px solid #aaa;
    }
  </style>
</head>
<body>
  <div id="root"></div>


  <script crossorigin src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
  <script crossorigin src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>
  <script src="./data.js"></script>
  <script src="./utils.js"></script>

  <script type="text/babel">

    class App extends React.Component {
      constructor() {
        super()
        this.state = {
          flowers: flowers
        }
      }
      // 修改数量
      handleComputer(index, type) {
        // 先浅拷贝一个对象
        const newFlowers = [...this.state.flowers]
        type === '+' ? newFlowers[index].count += 1 : newFlowers[index].count -= 1
        this.setState({
          flowers: newFlowers
        })
      }
      // 删除
      handleDelete(index) {
        const newFlowers = [...this.state.flowers]
        newFlowers.splice(index, 1)
        this.setState({
          flowers: newFlowers
        })
      }

      // 计算总价
      getTotalPrice() {
        return this.state.flowers.reduce((totalNum, item) => {
          return totalNum + item.price * item.count
        }, 0)
      }

      // 渲染列表页面
      renderFlowersList() {
        const { flowers } = this.state
        return (
          <div>
            <table>
              <thead>
                <th>序号</th>
                <th>花名</th>
                <th>出库日期</th>
                <th>价格</th>
                <th>数量</th>
                <th>操作</th>
              </thead>  
              <tbody>
                {
                  flowers.map((item, index) => {
                    return (
                      <tr key={item.id}>
                        <td>{ index + 1 }</td>
                        <td>{ item.name }</td>
                        <td>{ item.date }</td>
                        <td>{ formatPrice(item.price) }</td>
                        <td>
                          <button disabled={item.count <= 1} onClick={() => this.handleComputer(index, '-')}>-</button>
                          { item.count }
                          <button onClick={() => this.handleComputer(index, '+')}>+</button>
                        </td>
                        <td>
                          <button onClick={() => this.handleDelete(index)}>删除</button>  
                        </td>
                      </tr>
                    )
                  })
                }   
              </tbody>
            </table>
            <h2>总价格：{ formatPrice(this.getTotalPrice()) }</h2>
          </div>
        )
      }
      
      // 渲染空白页面
      renderFlowerEmpty() {
        return <h2>暂无鲜花咯~</h2>
      }
      render() {
        const { flowers } = this.state

        return (
          <div>{ flowers.length ? this.renderFlowersList() : this.renderFlowerEmpty() }</div>
        )
      }

    }
    
    const root = ReactDOM.createRoot(document.querySelector('#root'))
    root.render(<App/>)
  </script>
</body>
</html>